import {message } from 'antd';
import * as service from '../services/data';
export default {

  namespace: 'data',

  state: {
    performant:[],
    integral:[],
    list: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/' || pathname === '/integral' ){
          dispatch({ type: 'integral' });
        }else if(pathname==='/' || pathname==='/performant'){
           dispatch({ type: 'performant' });
         }
      });
    },
  },

  effects: {
    *integral({ payload }, { call, put }) {
      const integral = yield call(service.integral);
      integral && (yield put({type:'save',payload:{integral}}))
    },

    *performant({payload}, {call, put }){
      const performant = yield call(service.performant);
      performant && (yield put({type:'save',payload:{performant}}))
      },
    },

    *searchIntegral({payload}, {call, put, select}){
      login('3  >>>>>>>>',payload)
      const { area } = payload;
      const list = yield call(query);
      let data = list;
        if (area) {
          data = data.filter((item)=>item.area && item.area.match(area));
        }
        if(data){
          yield put({
            type: 'querySuccess',
            payload: {
              list: data,
            },
          })
        }
    },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    querySuccess (state, action) {
      const { list } = action.payload
      return { ...state,
        list
       }
    },
  },

};
